package DOM;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ArticleAnalyze {
	
	/**********************************************/
	public static String translateDate(String khmer){
		Pattern pattern = Pattern.compile(Constant.RegMonthKhmer+"|"+Constant.RegDateStateKhmer);
		Matcher matcher = pattern.matcher(khmer);
		if (matcher.find())
		{
			return translateDate(khmer.replaceAll(matcher.group(), Constant.dateDictionry().get(matcher.group())));
		}
		return khmer.trim().replaceAll("ទី|ខែ|ឆ្នាំ|ម៉ោង", "");
	}
	
	/**********************************************/
	public static TreeMap<String, String> spliteDate(String date){
		TreeMap<String, String> dateObj = new TreeMap<String, String>();
		String[] tmp;
		if(date.matches(".*,.*")){
			tmp = date.replaceAll(",", "").split(" +");
			dateObj.put("month", tmp[0]);
			dateObj.put("date", tmp[1]);
			dateObj.put("year", tmp[2]);
		}else if (date.matches(".*-.*-.*")){
			tmp = date.split("-");
			dateObj.put("year", tmp[0]);
			dateObj.put("month", Constant.dateDictionry().get(tmp[1]));
			dateObj.put("date", tmp[2]);
		}else{
			tmp = date.split(" +");
			dateObj.put("date", tmp[0]);
			dateObj.put("month", tmp[1]);
			dateObj.put("year", tmp[2]);
		}
		
//		System.out.println("leng = "+tmp.length);
		
		dateObj.put("time", (tmp.length==4)?tmp[3]:"00:00");
		dateObj.put("date-state", (tmp.length==5)?tmp[4]:"am");
		
		return dateObj;
	}
	
	/**********************************************/
	public static String DateObjToString(TreeMap<String, String> dateObj){
		return dateObj.get("month")+" "+dateObj.get("date")+", "+dateObj.get("year")+" "+dateObj.get("time")+" "+dateObj.get("date-state");
	}
	
	/**********************************************/
	public static Date dateAnalyzer(String str){
		String filteredDate = "";
		
		Date date = null;
		
		String RegPattern = "("+Constant.RegEnglish+")|("+Constant.RegKhmer+")";
		String RegFormat = "MMM dd, yyyy h:mm a";
		
		Pattern pattern = Pattern.compile(RegPattern);
		Matcher matcher = pattern.matcher(str);
		if (matcher.find())
		{
			String tmp = matcher.group().replaceAll("at ", "");
			if(tmp == null){
				filteredDate += DateObjToString(spliteDate(translateDate(matcher.group())));
			}else{
				filteredDate += DateObjToString(spliteDate(translateDate(tmp)));
			}
			
			SimpleDateFormat sdf = new SimpleDateFormat(RegFormat);
			
			try {
				date = sdf.parse(filteredDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
			
		return date;
	}
	
	public static Date parseSimpleDateFormat(String date, String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**********************************************/
	public static Date contentAnalyzer(String str){
		
		// code here
		
		return null;
	}
	
}
